﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;

namespace XNALibrary.Effects
{
    public class LightBulbEffect : CustomEffect
    {
        public bool LightBulbOn;
        public float LightPower;
        public bool PixelOn;
        public Color color;

        private float lightLife;
        private float deadLightWalking { get { return lightLife / 10; } }
        

        public LightBulbEffect(Effect e)
            : base(e)
        {
            LightBulbOn = true;
            LightPower = 2.0f;
            lightLife = 500;
            PixelOn = true;
            color = Color.White;
        }
        public override void SetupParameters()
        {
            throw new NotImplementedException();
        }

        public override void Update()
        {
            throw new NotImplementedException();
        }

        private void DrawModelMesh(RigidBody rb, ModelMesh mesh, RenderControl rc)
        {
            foreach (BasicEffect effect in mesh.Effects)
            {
                effect.World = rb.GetWorld(mesh);

                effect.View = rc.CurrentCamera.View;
                effect.Projection = rc.CurrentCamera.Projection;

                effect.EnableDefaultLighting();

                mesh.Draw();
            }
        }

        public override void DrawEffect(RigidBody body, RenderControl rc)
        {
            GraphicsDevice device = rc.graphicsDevice;
            Model model = body.Model;

            foreach (ModelMesh mesh in model.Meshes)
            {
                if (mesh.Name == "Sphere.002")
                {
                    foreach (ModelMeshPart meshPart in mesh.MeshParts)
                    {
                        // set the vertex source to the mesh's vertex buffer
                        device.Vertices[0].SetSource(
                            mesh.VertexBuffer, meshPart.StreamOffset, meshPart.VertexStride);

                        // set the vertex declaration
                        device.VertexDeclaration = meshPart.VertexDeclaration;

                        // set the current index buffer to the sample mesh's index buffer
                        device.Indices = mesh.IndexBuffer;

                        effect.Parameters["World"].SetValue(body.GetWorld(mesh));
                        effect.Parameters["View"].SetValue(rc.CurrentCamera.View);
                        effect.Parameters["Projection"].SetValue(rc.CurrentCamera.Projection);

                        effect.Parameters["ColorOn"].SetValue(color.ToVector4());
                        effect.Parameters["ColorOff"].SetValue(Color.White.ToVector4());
                        effect.Parameters["IsOn"].SetValue(LightBulbOn);
                        effect.Parameters["LightPower"].SetValue(LightPower);

                        effect.Parameters["AlphaValue"].SetValue(0.8f);

                        //if (LightPower <= deadLightWalking && LightPower > 0.0f)
                        //{
                        //    PixelOn = !PixelOn;
                        //    effect.Parameters["PixelOn"].SetValue(PixelOn);
                        //}
                        //else
                        
                        effect.Begin();
                        foreach (EffectPass pass in effect.CurrentTechnique.Passes)
                        {
                            pass.Begin();
                            device.DrawIndexedPrimitives(
                            PrimitiveType.TriangleList, meshPart.BaseVertex, 0,
                            meshPart.NumVertices, meshPart.StartIndex, meshPart.PrimitiveCount);
                            pass.End();
                        }
                        effect.End();
                    }
                }
                else
                {
                    DrawModelMesh(body, mesh, rc);
                }
            }
        }
    }
}
